﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Csharp结业设计
{
    public partial class admin_box2_add : Form
    {
        private string ConnectionString = "Data Source =LAPTOP-7I3EOIVU\\SQLEXPRESS ; Initial Catalog = Performance_management_system;" + "Integrated Security = true;";
        private SqlConnection conn = null;
        private SqlDataAdapter DataAdapter = null;
        private DataSet dataset = null;

        public admin_box2_add()
        {
            InitializeComponent();
        }

        private void showData()
        {
            try
            {
                conn.Open();
                DataAdapter = new SqlDataAdapter("SELECT * FROM Student_infor", conn);
                dataset = new DataSet();
                DataAdapter.Fill(dataset);
                dataGridView1.DataSource = dataset;
                dataGridView1.DataMember = dataset.Tables[0].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
                dataset.Dispose();
            }
        }

        private void admin_box2_add_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(ConnectionString);
            showData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSQL = "INSERT INTO Student_infor VALUES(";
            strSQL +=  textBox1.Text;
            strSQL += ",'" + textBox2.Text;
            strSQL += "','" + textBox3.Text;
            strSQL += "'," + textBox4.Text + ")";
            SqlCommand command = null;
            try
            {
                command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = strSQL;
                conn.Open();
                int n = command.ExecuteNonQuery();  //执行Insert语句
                if (n > 0) MessageBox.Show("成功插入数据!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn != null) conn.Close();
                command.Dispose();
            }
            showData();
        }
    }
}
